<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>

<body>
<div class="layuimini-container" >
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">

                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>

            </div>

        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-warn layui-btn-xs data-count-edit" lay-event="send">手动发送</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script type="text/html" id="state">
    {{#  if(d.state === 0 ){ }}
    <button class="layui-btn layui-btn-xs layui-btn-warm">进行中</button>
    {{#  } }}
    {{#  if(d.state === 1 ){ }}
    <button class="layui-btn layui-btn-xs layui-btn-normal">发送成功</button>
    {{#  } }}
    {{#  if(d.state === 2 ){ }}
    <button class="layui-btn layui-btn-xs layui-btn-danger">发送失败</button>
    {{#  } }}
</script>

<script type="text/html" id="status">
    {{#  if(d.way === 0 ){ }}
    <button class="layui-btn layui-btn-xs layui-btn-normal">通知</button>
    {{#  } }}
    {{#  if(d.way === 1 ){ }}
    <button class="layui-btn layui-btn-xs layui-btn-danger">交作业</button>
    {{#  } }}
</script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../api/urlConfig.js"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>

<script>


    function initTask(){
        $.ajax({
            url: ipURL + '/caption/task/listNoIncludeTiming',
            type: 'GET',
            dataType: 'json',
            beforeSend: function (request) {
                request.setRequestHeader("token", localStorage.token);
            },
            success: function (data) {

                $.each(data.data.data, function (index, value) {
                    $('#taskList').append(new Option(value.name,value.id));// 下拉菜单里添加元素
                });
                layui.form.render("select");//重新渲染 固定写法

            },
        });
    }
    function initEmailTable(){
        $.ajax({
            url: ipURL + '/caption/email-table/list',
            type: 'GET',
            dataType: 'json',
            beforeSend: function (request) {
                request.setRequestHeader("token", localStorage.token);
            },
            success: function (data) {

                $.each(data.data.data, function (index, value) {
                    $('#emailList').append(new Option(value.description,value.email));// 下拉菜单里添加元素
                });
                layui.form.render("select");//重新渲染 固定写法

            },
        });
    }

    $(function () {
        // 加载任务
        initTask()
        // 加载邮箱管理
        initEmailTable()
    })


    layui.use(['form', 'table','upload','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var upload = layui.upload;
        var laydate = layui.laydate;


        //执行一个laydate实例
        laydate.render({
            elem: '#test1' //指定元素
            ,type: 'datetime'
            ,min: 0
        });

        table.render({
            elem: '#currentTableId',
            url:ipURL+"/caption/timings",
            headers:{token:localStorage.token},
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data.data,//解析数据列表

                };

            },

            response: {
                statusCode: 0//规定成功的状态码，默认：0
            },
            text:{none: '无数据'},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'taskName', width:200, title: '定时任务'},
                {field: 'subject', width: 150, title: '主题'},
                {field: 'receive', width: 200, title: '接收方'},
                {field: 'stateName', width: 100, title: '发送状态',templet: '#state'},
                {field: 'stateName', width: 100, title: '邮件方式',templet: '#status'},
                {field: 'sendTime', width: 200, title: '发送时间'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {

            if (obj.event === 'add') {  // 监听添加操作
                $("#demo11").css('display','block');

                // 清空表单
                form.val("stuform", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": '' // "name": "value"
                    ,"subject": ''
                    ,"content": ''
                    ,"receive": ''
                    ,"sendTime": ''

                });
                layer.open({
                    type: 1,
                    title: '添加定时任务',
                    area: ['550px', '500px'],

                    content: $('#save'),
                    btn: ['提交', '退出'],
                    end:function(){
                        $('#save').css('display','none')
                    },
                    yes:function(){
                    },

                    success:function(layero,index){

                        layui.use('form',function(){
                            var form=layui.form;
                            layero.addClass('layui-form');
                            var submitBtn=layero.find('.layui-layer-btn0');
                            submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
                            layero.keydown(function(e){
                                if(e.keyCode==13){
                                    submitBtn.click();
                                }
                            });

                            form.on('submit(formVerify)',function(data){

                                $.ajax({
                                    url: ipURL + '/caption/timings',
                                    method: 'post',
                                    data: JSON.stringify(data.field),
                                    dataType: 'JSON',
                                    contentType: 'application/json;charset=UTF-8',
                                    beforeSend: function (request) {
                                        request.setRequestHeader("token", localStorage.token);
                                    },
                                    success: function (res) {
                                        layer.msg(res.message);
                                        if (res.state){
                                            layer.close(index);
                                            layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                                        }
                                    },
                                    error: function (res) {
                                        layer.msg(res.message);
                                    }
                                });


                                return false;
                            });
                        });
                    }
                })
            }  if (obj.event === 'delete') {  // 监听删除操作

                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var ids = "";
                for(i=0;i<data.length;i++){
                    ids+=data[i].id;
                    ids+=",";
                }

                $.ajax({
                    url: ipURL + '/caption/timings/'+ids,
                    method: 'delete',
                    beforeSend: function (request) {
                        request.setRequestHeader("token", localStorage.token);
                    },
                    success: function (res) {
                        layer.msg(res.message);
                        table.reload('currentTableId'); //刷新表格，参数为表格ID
                    },
                    error: function (data) {
                        layer.msg(data.message);
                    }
                });

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {

        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data)
            if (obj.event === 'edit') {
                $("#demo11").css('display','none');
                //给表单赋值
                form.val("stuform", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": data.id // "name": "value"
                    ,"subject": data.subject
                    ,"content": data.content
                    ,"receive": data.receive
                    ,"taskId": data.taskId
                    ,"sendTime": data.sendTime
                    ,"way": data.way
                });
                layer.open({
                    type: 1,
                    title: '修改定时任务',
                    area: ['550px', '500px'],

                    content: $('#save'),
                    btn: ['提交', '退出'],
                    end:function(){
                        $('#save').css('display','none')
                    },
                    yes:function(){
                    },

                    success:function(layero,index){

                        layui.use('form',function(){
                            var form=layui.form;
                            layero.addClass('layui-form');
                            var submitBtn=layero.find('.layui-layer-btn0');
                            submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
                            layero.keydown(function(e){
                                if(e.keyCode==13){
                                    submitBtn.click();
                                }
                            });

                            form.on('submit(formVerify)',function(data){

                                $.ajax({
                                    url: ipURL + '/caption/timings',
                                    method: 'put',
                                    data: JSON.stringify(data.field),
                                    dataType: 'JSON',
                                    contentType: 'application/json;charset=UTF-8',
                                    beforeSend: function (request) {
                                        request.setRequestHeader("token", localStorage.token);
                                    },
                                    success: function (res) {
                                        layer.msg(res.message);
                                        if (res.state){
                                            layer.close(index);
                                            layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                                        }

                                    },
                                    error: function (res) {
                                        layer.msg(res.message);
                                    }
                                });


                                return false;
                            });
                        });
                    }
                })
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除吗', function (index) {
                    $.ajax({
                        url: ipURL + '/caption/timings/'+data.id,
                        method: 'delete',
                        beforeSend: function (request) {
                            request.setRequestHeader("token", localStorage.token);
                        },
                        success: function (res) {
                            layer.msg(res.message);
                            if (res.state){
                                obj.del();
                                layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                            }
                        },
                        error: function (res) {
                            layer.msg(res.message);
                        }
                    });
                    layer.close(index);
                });

            }else if(obj.event === 'send'){
                $.ajax({
                    url: ipURL + '/caption/timings/artificial/send/'+data.id,
                    method: 'get',
                    beforeSend: function (request) {
                        request.setRequestHeader("token", localStorage.token);
                    },
                    success: function (res) {
                        layer.msg(res.message);
                    },
                    error: function (res) {
                        layer.msg(res.message);
                    }
                });
            }
        });

    });
</script>

</body>


<style type="text/css">
    .myData .layui-form-item{
        margin: 20px 100px 10px 45px;
    }
    .myData .layui-form-label{
        width: 80px;
    }
</style>


<form class="layui-form myData" action="save" method="post" lay-filter="stuform" id="save" style="display: none">
    <input type="hidden" name="id"  />

    <div class="layui-form-item" id="demo11">
        <label class="layui-form-label">定时任务：</label>
        <div class="layui-input-block">
            <select id="taskList" lay-filter="xmFilter" lay-search="" name="taskId">
                <option value="">直接选择或搜索选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮件主题：</label>
        <div class="layui-input-block">
            <input type="text" name="subject" lay-verify="required"  class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮件内容：</label>
        <div class="layui-input-block">
            <textarea name="content" placeholder="默认内容:共计{headCount}人,实交{practical}人。没有交作业的人:{noSubmitNames}" class="layui-textarea" ></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">接收方：</label>
        <div class="layui-input-block">
            <select id="emailList" lay-filter="xmFilter" class="teamList" lay-search="" name="receive">
                <option value="">直接选择或搜索选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">发送时间：</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" id="test1" name="sendTime" placeholder="yyyy-MM-dd HH:mm:ss">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮件方式</label>
        <div class="layui-input-block">
            <input type="radio" name="way" value="0" title="通知">
            <input type="radio" name="way" value="1" title="交作业" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <blockquote class="layui-elem-quote">通知:用于发送任务状态(供负责人查看作业记录)。
            <br>交作业:最后提交作业(携带作业)</blockquote>
    </div>
</form>

</html>